package com.bite.friend.aspect;


import com.bite.common.core.constants.Constants;
import com.bite.common.core.enums.ResultCode;
import com.bite.common.core.utils.ThreadLocalUtil;
import com.bite.common.security.exception.ServiceException;
import com.bite.friend.domain.user.User;
import com.bite.friend.manager.UserCacheManager;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class CheckUserStatusAspect {

    @Autowired
    private UserCacheManager userCacheManager;

    @Before("@annotation(com.bite.friend.aspect.CheckUserStatus)")
    public void methodCheck() {
        Long userId = ThreadLocalUtil.get(Constants.THREAD_LOCAL_USER_ID, Long.class);
        User user = userCacheManager.getByUserId(userId);
        if (user == null) {
            throw new ServiceException(ResultCode.FAILED_USER_NOT_EXISTS);
        }

        if (Constants.FALSE.equals(user.getStatus())) {
            throw new ServiceException(ResultCode.FAILED_USER_BANNED);
        }
    }

}
